package com.buss.purchase.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.buss.core.dic.CheckStatus;
import com.buss.core.persistence.BaseDao;
import com.buss.core.persistence.OrderBy;
import com.buss.core.persistence.Paginalbe;
import com.buss.core.persistence.ResultList;
import com.buss.core.utils.DateUtil;
import com.buss.core.utils.StringUtil;
import com.buss.purchase.dao.PurchaseOrderMasterDao;
import com.buss.purchase.model.PurchaseOrderMaster;

@Repository
public class PurchaseOrderMasterDaoImpl implements PurchaseOrderMasterDao {

	@Autowired
	private BaseDao<PurchaseOrderMaster> baseDao;
	
	@Override
	public PurchaseOrderMaster saveOrderMaster(PurchaseOrderMaster master){
		PurchaseOrderMaster orderMaster = baseDao.saveOrEdit(master);
		return orderMaster;
	}

	@Override
	public void editOrderMaster(PurchaseOrderMaster master) {
		baseDao.edit(master);
	}

	@Override
	public void removeOrderMaster(Long id) {
		String sql = "delete from PurchaseOrderMaster master where master.id = :id";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		baseDao.excuteHsql(sql, map);
	}

	@Override
	public PurchaseOrderMaster loadOrderMaster(Long id) {
		return baseDao.get(id, PurchaseOrderMaster.class);
	}

	@Override
	public PurchaseOrderMaster loadOrderMaster(String companyId,
			String orderNumber) {
		String sql = "select master from PurchaseOrderMaster master where master.branch.companyId = :companyId " +
				"and master.orderNumber = :orderNumber";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("companyId", companyId);
		map.put("orderNumber", orderNumber);
		return baseDao.loadHsql(sql, map);
	}

	public void updateCheckStatus(Long id,CheckStatus status,Long checkUserId,
			String checkUserName,Date checkDate){
		String sql = "update PurchaseOrderMaster master set master.checkStatus = :checkStatus ";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("checkStatus", status);
		if(checkUserId != null){
			sql += ",master.checkUserId = :checkUserId ";
			params.put("checkUserId", checkUserId);
		}
		if(StringUtil.isNotEmpty(checkUserName)){
			sql += ",master.checkUserName = :checkUserName ";
			params.put("checkUserName", checkUserName);
		}
		if(checkDate != null){
			sql += ",master.checkDate = :checkDate ";
			params.put("checkDate", checkDate);
		}
		sql += " where master.id = :id ";
		params.put("id", id);
		baseDao.excuteHsql(sql, params);
	}
	
	@Override
	public ResultList<PurchaseOrderMaster> queryOrderMaster(String companyId,Long supplierId,
			Long branchId, Long salesManId, Date orderStartDate,
			Date orderEndDate, CheckStatus checkStatus, Long checkUserId,
			Date checkStartDate, Date checkEndDate, Long createUserId,
			Date createStartDate, Date createEndDate,Paginalbe page,OrderBy orderBy) {
		String sql = "select master from PurchaseOrderMaster master where master.branch.companyId = :companyId ";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("companyId", companyId);
		if(branchId != null){
			sql += " and master.branch.id = :branchId ";
			params.put("branchId", branchId);
		}
		if(supplierId != null){
			sql += " and master.supplier.id = :supplierId ";
			params.put("supplierId", supplierId);
		}
		if(salesManId != null){
			sql += " and master.salesManId = :salesManId ";
			params.put("salesManId", salesManId);
		}
		if(orderStartDate != null){
			sql += " and master.orderDate >= :orderStartDate ";
			params.put("orderStartDate", orderStartDate);
		}
		if(orderEndDate != null){
			sql += " and master.orderDate < :orderEndDate ";
			params.put("orderEndDate", DateUtil.addDay(orderEndDate,1));
		}
		if(checkStatus != null){
			sql += " and master.checkStatus = :checkStatus ";
			params.put("checkStatus", checkStatus);
		}
		if(checkUserId != null){
			sql += " and master.checkUserId = :checkUserId ";
			params.put("checkUserId", checkUserId);
		}
		if(checkStartDate != null){
			sql += " and master.checkDate >= :checkStartDate ";
			params.put("checkStartDate", checkStartDate);
		}
		if(checkEndDate != null){
			sql += " and master.checkDate < :checkEndDate ";
			params.put("checkEndDate", DateUtil.addDay(checkEndDate, 1));
		}
		if(createUserId != null){
			sql += " and master.createUserId = :createUserId ";
			params.put("createUserId", createUserId);
		}
		if(createStartDate != null){
			sql += " and master.createDate >= :createStartDate ";
			params.put("createStartDate", createStartDate);
		}
		if(createEndDate != null){
			sql += " and master.createDate < :createEndDate ";
			params.put("createEndDate", DateUtil.addDay(createEndDate,1));
		}
		if(orderBy == null){
			orderBy = new OrderBy();
			orderBy.add("createDate", "desc");
		}
		sql += orderBy.aggOrder("master");
		
		return baseDao.queryHsql(sql, params, page);
	}

}
